<template>
  <el-dialog :title="!dataForm.id ? '新建' :'编辑'" :close-on-click-modal="false" append-to-body :visible.sync="visible"
    class="JNPF-dialog JNPF-dialog_center" lock-scroll width="600px">
    <el-row :gutter="15" class="">
      <el-form ref="elForm" :model="dataForm" :rules="rules" size="small" label-width="100px" label-position="right">
        <template v-if="!loading">
          <el-col :span="24">
            <el-form-item label="轮播类型" prop="rotateType">
              <el-select v-model="dataForm.rotateType" placeholder="请选择" clearable :style='{"width":"100%"}'>
                <el-option v-for="(item, index) in rotatetypeOptions" :key="index" :label="item.fullName"
                  :value="item.enCode" :disabled="item.disabled"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="排序" prop="sortCode">
              <el-input-number v-model="dataForm.sortCode" placeholder="排序" :min="1" :max="999" :step="1">
              </el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="轮播图标题" prop="rotateTitle">
              <el-input v-model="dataForm.rotateTitle" placeholder="请输入轮播图标题" maxlength="50" clearable :style='{"width":"100%"}'>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="链接类型" prop="linkType">
              <el-select v-model="dataForm.linkType" placeholder="请选择链接类型" clearable :style='{"width":"100%"}'>
                <el-option v-for="(item, index) in linktypeOptions" :key="index" :label="item.fullName"
                  :value="item.enCode" :disabled="item.disabled"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="链接地址" prop="linkUrl">
              <el-input v-model="dataForm.linkUrl" placeholder="请输入链接地址" maxlength="50" clearable :style='{"width":"100%"}'>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="描述" prop="description">
              <el-input v-model="dataForm.description" placeholder="请输入描述" maxlength="200" clearable :style='{"width":"100%"}'>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="图片" prop="picture">
              <JNPF-UploadImg v-model="dataForm.picture" :fileSize="5" sizeUnit="MB" :limit="1">
              </JNPF-UploadImg>
            </el-form-item>
          </el-col>
        </template>
      </el-form>
      <SelectDialog v-if="selectDialogVisible" :config="currTableConf" :formData="dataForm" ref="selectDialog"
        @select="addForSelect" @close="selectDialogVisible=false" />
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false"> 取 消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading"> 确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
  import request from '@/utils/request'
  import {
    getDataInterfaceRes
  } from '@/api/systemData/dataInterface'
  import {
    getDictionaryDataSelector
  } from '@/api/systemData/dictionary'
  export default {
    components: {},
    props: [],
    data() {
      return {
        visible: false,
        loading: false,
        btnLoading: false,
        selectDialogVisible: false,
        currTableConf: {},
        addTableConf: {},
        tableRows: {},
        currVmodel: "",
        dataForm: {
          rotateType: "",
          sortCode: 0,
          rotateTitle: '',
          linkType: "",
          linkUrl: '',
          description: '',
          picture: [],
        },
        rules: {
          rotateType: [{
            required: true,
            message: '轮播类型',
            trigger: 'blur'
          }],
          sortCode: [{
            required: true,
            message: '排序',
            trigger: ["blur", "change"]
          }],
          linkType: [{
            required: true,
            message: '链接类型',
            trigger: 'blur'
          }],
          picture: [{
            required: true,
            message: '请选择图片',
            trigger: 'change'
          }],
        },
        rotatetypeOptions: [],
        linktypeOptions: [],
      }
    },
    computed: {},
    watch: {},
    created() {
      this.getrotatetypeOptions();
      this.getlinktypeOptions();
    },
    mounted() {},
    methods: {
      getrotatetypeOptions() {
        getDictionaryDataSelector('366211763634438469').then(res => {
          this.rotatetypeOptions = res.data.list
        })
      },
      getlinktypeOptions() {
        getDictionaryDataSelector('366211869486088517').then(res => {
          this.linktypeOptions = res.data.list
        })
      },
      clearData() {
        this.dataForm.rotateType = "";
        this.dataForm.sortCode = 0;
        this.dataForm.rotateTitle = '';
        this.dataForm.linkType = "";
        this.dataForm.linkUrl = '';
        this.dataForm.description = '';
        this.dataForm.picture = [];
      },
      init(id) {
        this.dataForm.id = id || 0;
        this.visible = true;
        this.$nextTick(() => {
          this.$refs['elForm'].resetFields();
          if (this.dataForm.id) {
            this.loading = true
            request({
              url: '/api/zgyf/RotatePicture/' + this.dataForm.id,
              method: 'get'
            }).then(res => {
              this.dataInfo(res.data)
              this.loading = false
            });
          } else {
            this.clearData()
          }
        });
        this.$store.commit('generator/UPDATE_RELATION_DATA', {})
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['elForm'].validate((valid) => {
          if (valid) {
            this.request()
          }
        })
      },
      request() {
        this.btnLoading = true
        let _data = this.dataList()
        if (!this.dataForm.id) {
          request({
            url: '/api/zgyf/RotatePicture',
            method: 'post',
            data: _data
          }).then((res) => {
            this.$message({
              message: res.msg,
              type: 'success',
              duration: 1000,
              onClose: () => {
                this.visible = false
                this.btnLoading = false
                this.$emit('refresh', true)
              }
            })
          }).catch(() => {
            this.btnLoading = false
          })
        } else {
          request({
            url: '/api/zgyf/RotatePicture/' + this.dataForm.id,
            method: 'PUT',
            data: _data
          }).then((res) => {
            this.$message({
              message: res.msg,
              type: 'success',
              duration: 1000,
              onClose: () => {
                this.visible = false
                this.btnLoading = false
                this.$emit('refresh', true)
              }
            })
          }).catch(() => {
            this.btnLoading = false
          })
        }
      },
      openSelectDialog(key) {
        this.currTableConf = this.addTableConf[key]
        this.currVmodel = key
        this.selectDialogVisible = true
        this.$nextTick(() => {
          this.$refs.selectDialog.init()
        })
      },
      addForSelect(data) {
        for (let i = 0; i < data.length; i++) {
          let item = {
            ...this.tableRows[this.currVmodel],
            ...data[i]
          }
          this.dataForm[this.currVmodel].push(item)
        }
      },
      dataList() {
        var _data = JSON.parse(JSON.stringify(this.dataForm));
        _data.picture = Array.isArray(_data.picture) ? JSON.stringify(_data.picture) : '[]'
        return _data;
      },
      dataInfo(dataAll) {
        let _dataAll = dataAll
        _dataAll.picture = _dataAll.picture ? JSON.parse(_dataAll.picture) : []
        this.dataForm = _dataAll
      },
    },
  }
</script>
